<?php

namespace app\common\validate;
use app\common\model\User;
use cszchen\citizenid\Parser;


/**
 * Class TaskDropValidate
 * 任务取消验证类
 */
class UserValidate extends BaseValidate{
    /**
     * 验证规则
     */
    protected $rule = [
        'username|用户名'=> 'checkUser|checkCard',
        'real_name|真实姓名'       => 'require|length:2,30',
        'identity_card|身份证号码'       => 'require|length:10,30|checkCard',
        'identity_card_front|身份证正面'        => 'require|min:1|max:128',
        'identity_card_back|身份证背面'        => 'require|min:1|max:128',
        'gender|性别' =>'in:0,1',
        'bio|自我描述' =>'length:0,128',
        'age|年龄'        => 'number|max:200',
        'job|行业'        => 'max: 128',
        'address|地址'      => 'max: 128',
        'avatar|头像' => 'max:128',
        'nickname|昵称' =>'max:30',
        'password|密码' =>'require|alphaNum|length:6,20',
        'repassword|确认密码' =>'require|confirm:password|length:6,20',
        'department|部门' =>'length:0,128',
        'mobile|联系方式'  => 'isMobile'
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    protected function checkUser($value,$rule,$data)
    {
        if (isset($data['id'])) {
            $user = User::where('id', 'neq', $data['id'])->where('username', 'eq', $value)->count();
        }

        $user = User::where('username', 'eq', $value)->count();
        if ($user) {
            return '该用户已存在';
        }

        return true;
    }
    protected function checkCard($value,$rule,$data)
    {
        $parser = new Parser();
        $parser->setId($value);
        if (!$parser->isValidate()) {
            return '请正确填写身份证号码';
        } else {
            return true;
        }
    }
    /**
     * 验证场景
     */
    protected $scene = [
        'certification' => ['identity_card_front','identity_card_back'],
        'edit' => ['identity_card', 'real_name', '']
    ];
}
